#include "stack.h"

void StackInit(Stack* ps)
{
	ps->_a = NULL;
	ps->_capacity = 0;
	ps->_top = 0;
}

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->_capacity == ps->_top)
	{
		ps->_capacity = (ps->_capacity == 0 ? 4 : ps->_top * 2);
		STDataType* car = (STDataType*)realloc(ps->_a, sizeof(STDataType) * ps->_capacity);
		if (car == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		ps->_a = car;
	}
	ps->_a[ps->_top] = data;
	ps->_top++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->_top);
	ps->_top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->_top);
	return ps->_a[ps->_top - 1];
}

int StackSize(Stack* ps)
{
	assert(ps->_a);
	return ps->_top;
}

bool StackEmpty(Stack* ps)
{
	assert(ps);
	if (!ps->_a)
	{
		return true;
	}
	return false;
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = 0;
	ps->_top = 0;
}

void Print(Stack* ps)
{
	assert(ps);
	assert(ps->_a);
	for (int i = 0; i < ps->_top; i++)
	{
		printf("%d ", ps->_a[i]);
	}
	printf("\n");
}
